
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for friends_comments.
	''' </summary>
	Public Partial Class friends_comments
		Inherits BaseWebPage
		Protected lblNoComments1 As System.Web.UI.WebControls.Label
		Protected Button1 As System.Web.UI.WebControls.Button
		Private usrID As Integer = -1
		Protected isLoggedUser As Boolean = False
		Protected nCommentsCount As Integer = -1
		Protected action As String = ""
		Protected htPageConstants As New Hashtable()

		Private usr As User

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here
			' this.pageTemplate.PageTitle = PageTitles.FRIENDS_COMMENTS_PAGE;

			' Auto login from an external link
			'AutoLogin( Request );

			Me.initPageTitles(Constants.FRIENDS_COMMENTS_PAGE, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			Try
				If LoggedUser = Nothing Then
					Common.Utils.RedirectToLoginPage(Page)
				End If

				If Request.Params("usr") <> Nothing AndAlso Request.Params("usr") <> "" Then
					usrID = Convert.ToInt32(Request.Params("usr"))
					usr = Users.GetUserByID(usrID)

					If usr = Nothing Then
						Throw New Exception("This user doesn't exist")
					End If
				Else
					usrID = LoggedUser.ID
					usr = LoggedUser
				End If

				If usr.ID = LoggedUser.ID Then
					isLoggedUser = True
					litWho.Text = Me.GetPageConstText(htPageConstants, "CONST_YourTitle_TEXT")
					hlAddComments.Visible = False
					hlBackToProfile.NavigateUrl = "../" + Constants.MEMBERS
				Else
					litWho.Text = (Me.GetPageConstText(htPageConstants, "CONST_OneTitle_TEXT")).Replace("[PARAM]", usr.Name)
					hlAddComments.Visible = True
					'hlBackToProfile.Text = (this.GetPageConstText(htPageConstants, "CONST_BackToProfile_TEXT")).Replace("[PARAM]",  usr.Name);
					hlBackToProfile.NavigateUrl = "../" + Constants.MEMBERS + "?id=" + usrID
				End If

				'init action
				If Request.Params("action") <> Nothing AndAlso Request.Params("action") <> "" Then
					action = Request.Params("action")
				End If

				If usr <> Nothing AndAlso usr.ID <> LoggedUser.ID Then
					ucMemberShortcut.UserID = usr.ID
				Else
					trUserShortCut.Visible = False
				End If

				If Not Page.IsPostBack Then

					If usr.ApproveComments Then
						usr.ApproveAllComments()
					End If
				End If
				' init "No comments" label

				Dim NewComments As ArrayList = usr.NewComments
				Dim ApprovedComments As ArrayList = usr.Comments

				If isLoggedUser Then
					nCommentsCount = NewComments.Count + ApprovedComments.Count
				Else
					nCommentsCount = ApprovedComments.Count
				End If
				BindPager(nCommentsCount)

				'init Add Comments link
				hlAddComments.NavigateUrl = "../" + Constants.FRIENDS_ADD_COMMENT
				If usrID <> -1 Then
					hlAddComments.NavigateUrl += "?usr=" + usrID
				End If

				'bind comments
				If Not Page.IsPostBack Then
					dgMessages.VirtualItemCount = Convert.ToInt32(nCommentsCount)
					BindGrid()
				End If
			Catch
				lblNoComments.Visible = True
				lblNoComments.Text = Me.GetPageConstText(htPageConstants, "CONST_MsgError_TEXT")
			End Try
		End Sub
		Protected Function CreateDataSource() As ICollection
			Dim dt As New DataTable()

			Dim newCommentList As ArrayList, approvedCommentList As ArrayList, allComments As ArrayList
			allComments = New ArrayList()
			newCommentList = usr.NewComments
			approvedCommentList = usr.Comments

			Dim LoggedUsersList As ArrayList = Common.Utils.GetLoggedUsersIDs()

			If usr.ID = LoggedUser.ID Then
				allComments = newCommentList
			End If

			Dim i As Integer = 0
			While i < approvedCommentList.Count
				allComments.Add(approvedCommentList(i))
				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
			End While

			If allComments.Count <= 0 Then
				dgPager.FirstPageNumber = 1
				dgPager.LastPageNumber = 1
				dgPager.MaxPageNumber = 1
				dgPager.PagesRest = 1
				dgPager.dgListing.StartIndex = 0
				dgPager.dgListing.EndIndex = 0
				dgPagerUp.FirstPageNumber = 1
				dgPagerUp.LastPageNumber = 1
				dgPagerUp.MaxPageNumber = 1
				dgPagerUp.PagesRest = 1
				dgPagerUp.dgListing.StartIndex = 0
				dgPagerUp.dgListing.EndIndex = 0
				dgMessages.AllowSorting = False

				dgMessages.Visible = False
				lblNoComments.Visible = True
				trPagerUp.Visible = False
				trPager.Visible = False
			Else
				Dim startIndex As Integer = Convert.ToInt32(ViewState("StartIndex"))
				Dim endIndex As Integer = startIndex + Constants.MAX_USER_COMMENTS_ON_COMMENTS_PAGE
				Dim counter As Integer = 0

				endIndex = Math.Min(endIndex, allComments.Count)
				Dim t As Integer = (endIndex - startIndex)

				dgPager.dgListing.StartIndex = startIndex + 1
				dgPager.dgListing.EndIndex = endIndex
				dgPagerUp.dgListing.StartIndex = startIndex + 1
				dgPagerUp.dgListing.EndIndex = endIndex

				' initialize listing literals
				dgPager.dgListing.TotalCount = Convert.ToInt32(allComments.Count)
				dgPager.dgListing.UpdateControlsTexts()
				dgPagerUp.dgListing.TotalCount = Convert.ToInt32(allComments.Count)
				dgPagerUp.dgListing.UpdateControlsTexts()

				'initialize current page number

				Dim curCommentDate As DateTime

				' List user's inbox messages

				Dim dr As DataRow
				dt.Columns.Add(New DataColumn("SenderID", GetType(Integer)))
				dt.Columns.Add(New DataColumn("SenderName", GetType(String)))
				dt.Columns.Add(New DataColumn("SenderImageURL", GetType(String)))
				dt.Columns.Add(New DataColumn("OnlineImg", GetType(String)))
				dt.Columns.Add(New DataColumn("MemberURL", GetType(String)))
				dt.Columns.Add(New DataColumn("Date", GetType(String)))
				dt.Columns.Add(New DataColumn("Time", GetType(String)))
				dt.Columns.Add(New DataColumn("Comment", GetType(String)))
				dt.Columns.Add(New DataColumn("CommentID", GetType(String)))
				dt.Columns.Add(New DataColumn("ButtonsVisibility", GetType(Boolean)))
				dt.Columns.Add(New DataColumn("RejectText", GetType(String)))
				dt.Columns.Add(New DataColumn("SenderNameFull", GetType(String)))
				dt.Columns.Add(New DataColumn("FooterSpaceVisibility", GetType(Boolean)))
				dt.Columns.Add(New DataColumn("SenderFullName", GetType(String)))

				Dim temp As UserPicture
				Dim curSender As User
				' List user's NEW comments
				For Each usrComment As UserComment In allComments
					If counter >= startIndex AndAlso counter < endIndex Then
						dr = dt.NewRow()
						curCommentDate = usrComment.[Date]
						curSender = Users.GetUserByID(usrComment.PostedByUserID)
						dr(0) = usrComment.PostedByUserID
						dr(1) = getBeginning(HttpUtility.HtmlEncode(curSender.Name), Constants.MAX_NAME_LENGTH_ABOVE_IMAGE)
						dr(11) = curSender.Name
						temp = curSender.DefaultPicture
						If temp <> Nothing Then
							dr(2) = Pictures.GetPictureSrcFullURL(temp, True, "../")
						Else
							dr(2) = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
						End If

						If LoggedUsersList.Contains(curSender.ID) Then
							dr(3) = "../images/online_status.gif"
						Else
							dr(3) = "../images/spacer.gif"
						End If
						'if(curSender.Status == Users.STATUS_ACTIVE)
						If curSender.Status <> Users.STATUS_LOCKED Then
							dr(4) = "../" + Constants.MEMBERS + "?id=" + usrComment.PostedByUserID
						End If

						dr(5) = curCommentDate.DayOfWeek + ", " + Constants.ARR_MONTHS(curCommentDate.Month - 1) + " " + curCommentDate.Day + ", " + curCommentDate.Year
						dr(6) = curCommentDate.ToShortTimeString()
						dr(7) = usrComment.Comment
						dr(8) = usrComment.ID

						If (usrComment.CommentStatus = UserComment.Status.ucsNew) AndAlso (Not LoggedUser.ApproveComments) Then
							dr(9) = True
							dr(10) = "Reject"
							dr(12) = True
						Else
							dr(9) = False
							If action = Constants.FRIENDS_COMMENTS_EDIT_ACTION AndAlso (isLoggedUser OrElse LoggedUser.ID = usrComment.PostedByUserID) Then
								dr(10) = "Remove"
								dr(12) = True
							Else
								dr(10) = ""
								dr(12) = False
							End If
						End If
						dr(13) = curSender.Name
						dt.Rows.Add(dr)
					End If
					System.Math.Max(System.Threading.Interlocked.Increment(counter),counter - 1)
				Next
			End If

			Dim dv As New DataView(dt)
			Return dv
		End Function

		Protected Sub BindPager(ByVal count As Double)
			If count <= 0 Then
				dgMessages.Visible = False
				lblNoComments.Visible = True
				trPagerUp.Visible = False
				trPager.Visible = False
			Else
				dgPager.RedirectPage = Constants.FRIENDS_COMMENTS_PAGE

				dgPager.BeforePageNumberText = ""
				dgPagerUp.BeforePageNumberText = ""

				If action.Length > 0 AndAlso isLoggedUser Then
					dgPager.RedirectPage += "?action=" + action
ElseIf Not isLoggedUser Then
					dgPager.RedirectPage += "?usr=" + usr.ID
				End If
				dgPager.MaxPageNumber = Convert.ToInt32(Math.Ceiling(count / Constants.MAX_USER_COMMENTS_ON_COMMENTS_PAGE))
				dgPager.Action = "-1"
				dgPagerUp.RedirectPage = Constants.FRIENDS_COMMENTS_PAGE
				If action.Length > 0 AndAlso isLoggedUser Then
					dgPagerUp.RedirectPage += "?action=" + action
ElseIf Not isLoggedUser Then
					dgPagerUp.RedirectPage += "?usr=" + usr.ID
				End If
				dgPagerUp.MaxPageNumber = Convert.ToInt32(Math.Ceiling(count / Constants.MAX_USER_COMMENTS_ON_COMMENTS_PAGE))
				dgPagerUp.Action = "-1"

				' initialize listing literals
				dgPager.dgListing.TotalCount = Convert.ToInt32(count)
				dgPagerUp.dgListing.TotalCount = Convert.ToInt32(count)
				'initialize current page number
				If Convert.ToInt32(Request.Params("page")) = 0 OrElse dgPager.CurrentPageNumber = 1 Then
					dgPager.CurrentPageNumber = 1
					dgPagerUp.CurrentPageNumber = 1
					ViewState("StartIndex") = 0
				Else
					dgPager.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
					dgPagerUp.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
					ViewState("StartIndex") = (dgPager.CurrentPageNumber - 1) * Constants.MAX_USER_COMMENTS_ON_COMMENTS_PAGE
				End If

				dgPager.PagesRest = dgPager.MaxPageNumber - dgPager.FirstPageNumber + 1
				dgPager.LastPageNumber = Math.Min(dgPager.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)
				dgPagerUp.PagesRest = dgPagerUp.MaxPageNumber - dgPagerUp.FirstPageNumber + 1
				dgPagerUp.LastPageNumber = Math.Min(dgPagerUp.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)
			End If
		End Sub

		Protected Sub BindGrid()
			dgMessages.PageSize = Constants.MAX_USER_COMMENTS_ON_COMMENTS_PAGE
			dgMessages.DataSource = CreateDataSource()
			dgMessages.DataBind()
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.dgMessages.ItemCommand, AddressOf Me.dgMessages_ItemCommand
		End Sub
#End Region

		Private Sub dgMessages_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
			Dim lbPushed As LinkButton = DirectCast(e.CommandSource, LinkButton)
			Dim cmd As String = e.CommandName
			Select Case cmd
				Case "Reject"
					' reject/remove comment
					usr.RemoveComment(Convert.ToInt32(e.CommandArgument))
					Exit Select
				Case "Approve"
					'approve comment
					usr.ApproveComment(Convert.ToInt32(e.CommandArgument))
					Exit Select
			End Select
			BindGrid()
		End Sub

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
		Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated_Form1
			End Get
		End Property
		'set { Migrated_Form1 = value; }
	End Class
End Namespace






